﻿package bdz.projects.slab.sections {
	import as3.common.ui.menu.core.MenuComponent;
	import as3.common.ui.menu.events.MenuComponentEvent;
	import as3.common.ui.menu.TweenedGridList;
	import bdz.dms.data.DMSCollection;
	import bdz.dms.vo.CoreDataVO;
	import bdz.dms.vo.SectionVO;
	import bdz.dms.vo.ContentVO;
	import bdz.projects.slab.core.SlabSection;
	import bdz.projects.slab.events.SlabSiteEvent;
	import bdz.projects.slab.renderers.MediaRenderer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.*;
	import bdz.common.utils.BitmapUtils;
	import bdz.common.utils.TweenLite;
	import flash.text.*;
	import flash.utils.*;
	import as3.common.ui.menu.AnimatedItem;
	import bdz.common.utils.DateUtils;
	import fl.transitions.easing.*;
	import bdz.common.events.DispatchChannel;
	/**
	* ...
	* @author Default
	*/
	public class SlabArtistWorks extends SlabSection {
		private var list:TweenedGridList;
		private var _info:DMSCollection;
		private var arrLeft:SimpleButton;
		private var arrRight:SimpleButton;
		private var listCover:MovieClip;
		private var subMenu:MenuComponent;
		private var _ginfo:SectionVO;
		private var header:GalleryTitle;
		private var footer:ImageDescription;
		private var exItems:Array;
		private var actArtist:String;
		private var acount:Number = 0;
		private var outMask:MovieClip;
		private var isDocumentation:Boolean = false;
		private var infoText:InfoText;
		private var artistVO:SectionVO;
		private var viewVid:ViewVideo;
		private var dvids:DMSCollection;
		private var nocontent:HomeDesc;
		function SlabArtistWorks() {
			
		}
		override protected function initializeComponent(evt:Event):void {
			listCover = new MovieClip();
			listCover.x = 14;
			listCover.y = 40;
			outMask = new MovieClip();
			outMask.x = 14;
			outMask.y = 40;
			BitmapUtils.createBounds(listCover, 620, 300, 0);
			BitmapUtils.createBounds(outMask, 620, 300, 0);
			addChild(listCover);
			addChild(outMask);
			
			if (isDocumentation) {
				exItems = _info.whereProperty("tag", "documentation_image");
				if(exItems.length){
					createImageList(exItems)
					infoText = new InfoText();
					infoText.description = "Documentation: " + artistVO.section_name;
					infoText.x = listCover.x - 3;
					infoText.y = Math.ceil(listCover.y + listCover.height)+5;
					addChild(infoText);
					createExtraDetails();
				}else {
					nocontent = new HomeDesc();
					nocontent.x = listCover.x;
					nocontent.y = Math.ceil(listCover.y + listCover.height/2)-30;
					nocontent.description = "No details yet.";
					addChild(nocontent);
				}
			}else {
				createSubMenu();
			}
			super.initializeComponent(evt);
		}
		private function createImageList(source:Array):void {
			trace(source);
			var append:Boolean = false;
			if (list && contains(list)) {
				append = true;
				var oldlist = list;
				oldlist.mask = outMask;
				TweenLite.to(oldlist, .9, { x:oldlist.x - listCover.width*2, ease:Strong.easeInOut, onComplete:removeChild, onCompleteParams:[oldlist] } );
			}
			var tmp:MediaRenderer = new MediaRenderer(new Object(), new TextFormat());
			list = new TweenedGridList("bdz.projects.slab.renderers.MediaRenderer");
			list.addEventListener("listReady", listReady);
			list.addEventListener(MenuComponentEvent.CLICK, itemClicked);
			list.xpad = 20;
			list.x = !append ? listCover.x : listCover.y+listCover.width*2;
			list.y =  listCover.y;
			list.labelField = "section_id";
			list.dimOthers = true;
			list.viewableColumns = 2;
			list.showUpToEdge = true;
			list.maxrows = 1;
			list.scrollDuration = .8;
			list.delay = .01;
			list.edgeDelay = .03;
			list.arrayItems = source;
			list.showUpToEdge = true;
			list.offsetEdges = false;
			list.mask = listCover;
			list.easing = Strong.easeInOut;
			addChild(list);
			if (append) {
				TweenLite.to(list, 1, { x:listCover.x, ease:Strong.easeInOut} );
			}
			
			
		}
		private function setUpArrows():void{
			if (arrLeft && contains(arrLeft)) {
				removeChild(arrLeft);
				removeChild(arrRight);
			}
			if(list && list.maxhscroll>0){
				arrLeft = new arrowLeft();
				arrRight = new arrowRight();
				arrLeft.x = listCover.x-20;
				arrRight.x = listCover.x+listCover.width+20;
				arrLeft.y = arrRight.y = listCover.y+(listCover.height/2);
				arrLeft.visible = false;
				arrRight.alpha = arrLeft.alpha = 0;
				TweenLite.to(arrRight, .8, {alpha:1});
				TweenLite.to(arrLeft, .8, {alpha:1});
				addChild(arrLeft);
				addChild(arrRight);
				arrRight.addEventListener(MouseEvent.CLICK, scrollRight);
				arrLeft.addEventListener(MouseEvent.CLICK, scrollLeft);
				
			}
		}
		private function scrollRight(evt:MouseEvent):void {
			list.scrollLeft();
			if(list.curhscroll == list.maxhscroll){
				arrRight.visible = false;
			}
			if(list.curhscroll){
				arrLeft.visible = true;
			}
			createFooter();
		}
		private function scrollLeft(evt:MouseEvent):void {
			list.scrollRight();
			if(list.curhscroll < list.maxhscroll){
				arrRight.visible = true;
			}
			if(!list.curhscroll){
				arrLeft.visible = false;
			}
			createFooter()
		}
		private function createSubMenu():void {
			var tmp = new AnimatedItem(new Object(), new TextFormat());
			tmp = null;
			
			var menuStyle:Object = {embedFonts:true, autoSize:"left", border:false, background:false, backgroundColor:0x222222, antiAliasType:AntiAliasType.ADVANCED, gridFitType:GridFitType.NONE};
			var menuFontFormat:TextFormat = new TextFormat();
			var fontObj:Object = getDefinitionByName("hellvetica");
			menuFontFormat.color = 0x999999;
			menuFontFormat.font = new fontObj().fontName;
			menuFontFormat.size = 18;
			
			subMenu = new MenuComponent("as3.common.ui.menu.AnimatedItem");
			subMenu.y = 345;
			subMenu.x = 10;
			subMenu.slow_generation = true;
			subMenu.orientation = "horizontal";
			subMenu.menuSeparator = "artistSeparator";
			subMenu.labelField = "menu_label";
			subMenu.menuTextFieldStyle = menuStyle;
			subMenu.menuFontStyle = menuFontFormat;
			subMenu.rollOverColor = 0x282725;
			subMenu.upperCaseLabel = false;
			subMenu.xpad = 17;
			
			subMenu.addEventListener(MenuComponentEvent.CLICK, subMenuClicked);
			
			addChild(subMenu);
			subMenu.items = getArtistMenu();
			createExtraDetails();
		}
		private function getDateString(str:String, incyear:Boolean = true):String {
			var dte:Date = new Date(str);
			var dstr:String = "";
			if (DateUtils.getMonthString(dte.getMonth()) == null) {
				return null;
			}
			dstr += DateUtils.getMonthString(dte.getMonth()) + " ";
			
			dstr += dte.getDate() + " ";
			if (incyear) {
				dstr += dte.getFullYear();
			}
			return dstr;
		}
		private function createExtraDetails():void {
			header = new GalleryTitle();
			
			if (getDateString(_ginfo.getProperty("start_date"), false) == null) {
				header.description = _ginfo.section_name;
			}else{
				header.description = _ginfo.section_name + " - " + getDateString(_ginfo.getProperty("start_date"), false) + " to " + getDateString(_ginfo.getProperty("end_date"));
			}
			try{
				header.y = subMenu.y + 25;
				header.x = subMenu.x;
			}catch (e:Error) {
				header.y = infoText.y + 25;
				header.x = infoText.x;
			}
			addChild(header);
			if (isDocumentation) {
				dvids = _info.whereProperty("tag", "documentation_video")
				if (dvids.length) {
					if(dvids[0].getProperty("video") != ""){
						viewVid = new ViewVideo();
						viewVid.x = infoText.x + 4;
						viewVid.y = header.y + header.height + 18;
						viewVid.alpha = 0;
						viewVid.addEventListener(MouseEvent.CLICK, onViewVideo);
						TweenLite.to(viewVid, .9, { delay:1.4, alpha:1, ease:Strong.easeOut } );
						addChild(viewVid);
					}
				}
			}
		}
		private function onViewVideo(evt:MouseEvent):void {
			
			DispatchChannel.getChannel().dispatchEvent(new SlabSiteEvent(SlabSiteEvent.SHOW_VIDEO, dvids[0].getProperty("video").toString()));
		}
		private function createFooter():void {
			if (footer && contains(footer)) {
				removeChild(footer);
			}
			if(exItems[list.curhscroll].content_label && exItems[list.curhscroll].content_label != ""){
				footer = new ImageDescription();
				
				if (acount > 1) {
					
					footer.description = exItems[list.curhscroll].content_label + " by " + ContentVO(exItems[list.curhscroll]).getPropertyNameByKey("tag").toUpperCase();
				}else {
					footer.description = exItems[list.curhscroll].content_label;
				}
				try{
					footer.x = Math.ceil(subMenu.x + 4);
				}catch (e:Error) {
					footer.x = Math.ceil(infoText.x + 4);
				}
				try{
					footer.y = Math.ceil(header.y + 25);
				}catch (e:Error) {
					footer.y = Math.ceil(infoText.y + 25);
				}
				addChild(footer);
			}
		}
		private function subMenuClicked(evt:MenuComponentEvent):void {
			actArtist = evt.data.menu_label;
			exItems = new Array();
			var works:DMSCollection = _info.whereProperty("tag", "artist_works");			
			for each(var cvo:ContentVO in works) {
				if (actArtist == cvo.getPropertyNameByKey("tag")) {	
					exItems.push(cvo);
				}
			}
			
			
			createImageList(exItems);
		}
		private function getArtistMenu():XMLList {
			var xm:XML = <data/>;
			xm.data_record = "";
			var arr:DMSCollection =  new DMSCollection();
			var counter:Number = 0;
			for each(var cvo:* in _info) {
				if (cvo is SectionVO) {
					acount++;
					var xnode = <data_record/>;
					xnode.section_id = cvo.section_id;
					xnode.menu_label = cvo.section_name;
					xm.data_record[counter] = xnode;
					counter++;
				}
			}
			return xm.data_record;
		}
		private function getActiveArtist():SectionVO {
			for each(var cvo:* in _info) {
				if (cvo is SectionVO) {
					if (SectionVO(cvo).section_name == actArtist) {
						return cvo as SectionVO;
					}
				}
			}
			return null;
		}
		private function listReady(evt:Event):void {
			setUpArrows();
			setTimeout(createFooter, 300);
		}
		private function itemClicked(evt:MenuComponentEvent):void {
			
		}
		public function get activeArtist():SectionVO {
			return getActiveArtist();
		}
		public function set ginfo(ginf:SectionVO):void {
			_ginfo = ginf;
		}
		public function get ginfo():SectionVO {
			return _ginfo;
		}
		public function set info(dcoll:DMSCollection):void {
			_info = dcoll;
		}
		public function get info():DMSCollection {
			return _info;
		}
		public function set forDocumentation(b:Boolean):void {
			isDocumentation = b;
		}
		public function get forDocumentation():Boolean {
			return isDocumentation;
		}
		public function set artist(avo:SectionVO):void {
			artistVO = avo;
		}
		public function get artist():SectionVO {
			return artistVO;
		}
	}
	
}